package at.codebase.db.entities;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;

/**
 * This class is used for the Item rating implementation
 * 
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 * 
 */
@Entity
public class EItemFeedback {

	@Id
	private Long itemFeedbackId;
	@Index
	private Key<EItem> item;
	@Load
	@Index
	private Ref<EUser> author;
	private long timeCreate;

	private EItemFeedbackRating feedbackRating;
	private String feedbackContent;

	/*
	 * Enums
	 */
	public enum EItemFeedbackRating {
		NO_RATING, RUBY_1, RUBY_2, RUBY_3, RUBY_4, RUBY_5
	}

	/*
	 * Objectify
	 */
	@SuppressWarnings("boxing")
	public static Key<EItemFeedback> key(Long feedbackId) {
		return Key.create(EItemFeedback.class, feedbackId);
	}

	public static Ref<EItemFeedback> ref(Long feedbackId) {
		Key<EItemFeedback> key = key(feedbackId);
		return Ref.create(key);
	}

	/*
	 * Constructors
	 */
	public EItemFeedback() {
	}

	public EItemFeedback(Key<EItem> item, Ref<EUser> author, long timeCreate,
			EItemFeedbackRating feedbackRating, String feedbackContent) {
		super();
		this.item = item;
		this.author = author;
		this.timeCreate = timeCreate;
		this.feedbackContent = feedbackContent;
		this.feedbackRating = feedbackRating;
	}

	public static EItemFeedback create(Long itemId, Long userId,
			EItemFeedbackRating rating, String text) {
		return new EItemFeedback(EItem.key(itemId), EUser.ref(userId),
				System.currentTimeMillis(), rating, text);
	}

	public Long getId() {
		return itemFeedbackId;
	}

	public void setId(Long id) {
		this.itemFeedbackId = id;
	}

	public Ref<EUser> getAuthor() {
		return author;
	}

	public void setAuthor(Ref<EUser> author) {
		this.author = author;
	}

	public Key<EItem> getItem() {
		return item;
	}

	public void setItem(Key<EItem> item) {
		this.item = item;
	}

	public String getFeedbackContent() {
		return feedbackContent;
	}

	public void setFeedbackContent(String feedbackContent) {
		this.feedbackContent = feedbackContent;
	}

	public double getFeedbackRatingValue() {
		if (feedbackRating != null) {
			if (feedbackRating.equals(EItemFeedbackRating.RUBY_1)) {
				return 1.0d;
			} else if (feedbackRating.equals(EItemFeedbackRating.RUBY_2)) {
				return 2.0d;
			} else if (feedbackRating.equals(EItemFeedbackRating.RUBY_3)) {
				return 3.0d;
			} else if (feedbackRating.equals(EItemFeedbackRating.RUBY_4)) {
				return 4.0d;
			} else if (feedbackRating.equals(EItemFeedbackRating.RUBY_5)) {
				return 5.0d;
			} else {
				return -1.0d;
			}
		}

		return -1.0d;

	}

	public void setFeedbackRating(EItemFeedbackRating feedbackRating) {
		this.feedbackRating = feedbackRating;
	}

	public long getTimeCreate() {
		return timeCreate;
	}

	public void setTimeCreate(long timeCreate) {
		this.timeCreate = timeCreate;
	}

	public EItemFeedbackRating getFeedbackRating() {
		return feedbackRating;
	}

	@Override
	public String toString() {
		return "EItemFeedback [itemFeedbackId=" + itemFeedbackId + ", item="
				+ item + ", author=" + author + ", timeCreate=" + timeCreate
				+ ", feedbackRating=" + feedbackRating + ", feedbackContent="
				+ feedbackContent + "]";
	}
	
}
